<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb20">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="120px">
      <el-form-item label="工单编号" prop="workOrderId">
        <el-input v-model="queryParams.workOrderId" placeholder="请输入工单编号" clearable style="width: 240px"
                  @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="申请时间">
        <el-date-picker v-model="dateRange" type="daterange" range-separator="~" start-placeholder="开始日期"
                        end-placeholder="结束日期" style="width: 240px"
                        value-format="yyyy-MM-dd"
        />
      </el-form-item>
      <el-form-item label="申请人姓名" prop="reqUserName">
        <el-input v-model="queryParams.reqUserName" placeholder="请输入姓名" clearable style="width: 240px"
                  @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="reqPhone">
        <el-input v-model="queryParams.reqPhone" placeholder="请输入手机" clearable style="width: 240px"
                  @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-tabs v-model="activeName" @tab-click="handleTabsClick">
      <el-tab-pane label="全部" name="all"></el-tab-pane>
      <el-tab-pane :label="'待审核('+statistics['0']+')'" name="0"></el-tab-pane>
      <el-tab-pane :label="'已同意('+statistics['1']+')'" name="1"></el-tab-pane>
      <el-tab-pane :label="'已拒绝('+statistics['2']+')'" name="2"></el-tab-pane>
    </el-tabs>
    <el-table v-loading="loading" :data="list">
      <el-table-column label="序号" align="center" key="lockName" prop="lockName" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="工单编号" align="center" key="workOrderId" prop="workOrderId"
                       :show-overflow-tooltip="true"
      ></el-table-column>
      <el-table-column label="工单类型" align="center" key="workOrderType" prop="workOrderType"
                       :show-overflow-tooltip="true"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.work_order_type"
            :value="scope.row.workOrderType"
          />
        </template>
      </el-table-column>
      <el-table-column label="房源" align="center" key="premisesAddress" prop="premisesAddress"
                       :show-overflow-tooltip="true"
      />
      <el-table-column label="申请人" align="center" key="reqUserName" prop="reqUserName"
                       :show-overflow-tooltip="true"
      />
      <el-table-column label="申请人手机号" align="center" key="reqPhone" prop="reqPhone"
                       :show-overflow-tooltip="true"
      />
      <el-table-column label="申请时间" align="center" key="createTime" prop="createTime"
                       :show-overflow-tooltip="true"
      >
        <template slot-scope="scope">
          {{ scope.row.createTime|formatDate('YYYY-MM-DD HH:mm') }}
        </template>
      </el-table-column>
      <el-table-column label="密码使用时段" align="center" key="timeRange" prop="timeRange"
                       :show-overflow-tooltip="true"
      >
        <template slot-scope="scope">
          <span v-if="scope.row.pwdStartTime || scope.row.pwdEndTime">
            {{ scope.row.pwdStartTime|formatDate('YYYY-MM-DD HH:mm') }}-{{ scope.row.pwdEndTime|formatDate('HH:mm') }}
          </span>
          <span v-else>
            -
          </span>
        </template>
      </el-table-column>
      <el-table-column label="审核状态" align="center" key="pwdAuditState" prop="pwdAuditState"
                       :show-overflow-tooltip="true"
      >
        <template slot-scope="scope">
          {{ pwdAuditStateMapping[scope.row.pwdAuditState] }}
        </template>
      </el-table-column>
      <el-table-column label="审核信息" align="center" key="remark" prop="remark"
                       :show-overflow-tooltip="true"
      >
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope" v-if="scope.row.pwdAuditState === '0'">
          <el-button type="text" size="mini" @click="handleAudit(scope.row,'1')">同意</el-button>
          <el-button type="text" size="mini" @click="handleAudit(scope.row,'2')">拒绝</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
                @pagination="getList"
    />

    <!--  添加维修项目弹出层   -->
    <Audit v-model="open" :title="title" :data="form" @success="getList"/>
  </div>
</template>

<script>
import Audit from './components/audit.vue'
import { debounce } from '@/utils'
import { countPwdAuditState, pwdRecordList } from '@/api/houseManagement/pwdRecord'

export default {
  name: 'Index',
  dicts: ['work_order_type'],
  components: {
    Audit
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      list: null,
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 表单参数
      form: {},
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        workOrderId: undefined,
        reqUserName: undefined,
        reqPhone: undefined,
        pwdAuditState: undefined
      },
      pwdAuditStateMapping: {
        '0': '待审核',
        '1': '同意',
        '2': '拒绝'
      },
      // 选项
      activeName: 'all',
      statistics: {}
    }
  },
  watch: {},
  created() {
    this.getList()
    this.getCount()
  },
  methods: {
    /** 查询用户列表 */
    getList() {
      this.loading = true
      pwdRecordList(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.list = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
      this.getCount()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = []
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        workOrderId: undefined,
        reqUserName: undefined,
        reqPhone: undefined,
        pwdAuditState: undefined
      },
        this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 日期范围 */
    handleTabsClick(tab, event) {
      this.activeName = tab.name
      this.queryParams.pwdAuditState = tab.name === 'all' ? undefined : tab.name
      this.handleQuery()
    },
    handleAudit(row, state) {
      this.form = { ...row, 'state': state }
      this.title = '提示'
      this.open = true

    },
    // 密码审核状态统计
    getCount() {
      countPwdAuditState(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.statistics = response.data
      })
    }
  }
}
</script>

<style lang="scss" scoped></style>
